package com.cweb.framework.pojo;

import com.cweb.framework.util.ErrorEnum;

public class BaseResponse {
    public enum Status {
        /**
         * 请求成功，结果正常返回
         */
        SUCCESS,
        /**
         * 请求失败，结果返回异常信息
         */
        ERROR,
        /**
         * 其他
         */
        EXCEPTION
    }

    public BaseResponse() {
    }

    public BaseResponse(Object detail) {
        setDetail(detail);
    }

    private Status status = Status.SUCCESS;
    private Integer count; // 查询结果的记录条数
    private Object detail;

    public void setError(ErrorEnum error) {
        setStatus(Status.ERROR);
        if (null != error) {
            this.detail = error.getCode() + ":" + error.getMsg();
        }
    }

    public void setError(String msg) {
        setStatus(Status.ERROR);
        this.detail = msg;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Object getDetail() {
        return detail;
    }

    public void setDetail(Object detail) {
        this.detail = detail;
    }

    public static BaseResponse errorMessage(String code, String message) {
        BaseResponse response = new BaseResponse();
        response.setError(code + ":" + message);
        return response;
    }

    public static BaseResponse errorMessage(ErrorEnum code) {
        BaseResponse response = new BaseResponse();
        response.setError(code);
        return response;
    }

    public static BaseResponse errorMessage(String message) {
        BaseResponse response = new BaseResponse();
        response.setError(message);
        return response;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

}
